package enumdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 87111
 * Date: 2025-09-19
 * Time: 19:25
 */
public enum TestEnum {
    RED("red",1),YELLOW("yellow",2),BLACK("black",3);
    public String color;
    public int key;
    TestEnum(String color,int key) {
        this.color = color;
        this.key = key;
    }

    public static void reflectPrivateConstructor() {
        Class<?> tmp;
        try {
            tmp = Class.forName("enumdemo.TestEnum");
            Constructor<?> constructor =
                    tmp.getDeclaredConstructor(String.class,int.class,String.class,int.class);
            constructor.setAccessible(true);
            TestEnum testEnum = (TestEnum)constructor.newInstance("绿色",4,"子类参数",88);
            System.out.println(testEnum);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        reflectPrivateConstructor();
    }

    public static void main2(String[] args) {
        TestEnum[] testEnum1 = TestEnum.values();
        for (int i = 0; i < testEnum1.length; i++) {
            System.out.print(testEnum1[i] + " ");
        }
        System.out.println();
        System.out.println(RED.ordinal());
        System.out.println("===============");

        TestEnum tmp = TestEnum.valueOf("RED");
        System.out.println(tmp);

        TestEnum testEnum2 = TestEnum.RED;
        TestEnum testEnum3 = TestEnum.YELLOW;
        System.out.println(testEnum2.compareTo(testEnum3));
    }
    public static void main1(String[] args) {
        TestEnum testEnum = TestEnum.RED;
        switch(testEnum) {
            case RED:
                System.out.println("red");
                break;
            case YELLOW:
                System.out.println("yellow");
                break;
            case BLACK:
                System.out.println("black");
                break;
            default:
                break;
        }
    }
}
